using UnityEngine;
using UnityEngine.SceneManagement;

public class OpticalAimer : MonoBehaviour
{
	private bool _sniperMode;

	private float normalFOV;

	private float zoomedFOV = 15f;

	private TPSCamera cam;

	private Camera camLod;

	private PlayerBehavior pBehavior;

	public bool sniperMode
	{
		get
		{
			return _sniperMode;
		}
		set
		{
			_sniperMode = value;
			if (pBehavior != null)
			{
				pBehavior.SwitchAimMod(value);
			}
		}
	}

	private void Start()
	{
		if (SceneManager.GetActiveScene().name != "Lobby")
		{
			pBehavior = base.transform.GetComponentInParent<PlayerBehavior>();
			if (settings.offlineMode)
			{
				if (pBehavior == null)
				{
					Object.Destroy(this);
				}
			}
			else if (pBehavior == null)
			{
				Object.Destroy(this);
			}
			else if (pBehavior.photonView != null && !pBehavior.photonView.isMine)
			{
				Object.Destroy(this);
			}
			if (pBehavior != null)
			{
				cam = GameController.instance.tpsCamera;
				camLod = NGUITools.FindCameraForLayer(28);
				normalFOV = cam.GetComponent<Camera>().fieldOfView;
			}
			OpticalAimHUD.instance.SetActiveModeAim(false);
			OpticalAimHUD.instance.SetActiveButAim(true);
			OpticalAimHUD.instance.stateOpticalActive = true;
		}
		else
		{
			base.enabled = false;
		}
	}

	private void OnEnable()
	{
	}

	private void SwitchRenderers(bool on)
	{
		if (pBehavior != null)
		{
			pBehavior.SetVisibleOnlyMeshObjPlayer(on);
		}
	}

	public void ShowSniperAimer()
	{
		if (!sniperMode)
		{
			if (BCWDebug.isDebugBuild)
			{
				BCWDebug.Log("ShowSniperAimer");
			}
			sniperMode = true;
			base.transform.localPosition = base.transform.forward * -3f;
			cam.GetComponent<Camera>().fieldOfView = zoomedFOV;
			if (camLod != null)
			{
				camLod.fieldOfView = zoomedFOV;
			}
			WalkWindow.instance.opticalAimHUD.SetActiveModeAim(true);
			SwitchRenderers(false);
		}
	}

	public void HideSniperAimer()
	{
		if (!sniperMode)
		{
			return;
		}
		sniperMode = false;
		SwitchRenderers(true);
		if (cam != null)
		{
			cam.GetComponent<Camera>().fieldOfView = normalFOV;
			if (camLod != null)
			{
				camLod.fieldOfView = normalFOV;
			}
		}
		if (WalkWindow.instance != null)
		{
			WalkWindow.instance.opticalAimHUD.SetActiveModeAim(false);
		}
		base.transform.localPosition = Vector3.zero;
	}

	private void OnDisable()
	{
		HideSniperAimer();
	}

	private void OnDestroy()
	{
		_sniperMode = false;
		SwitchRenderers(true);
		if (cam != null)
		{
			if (camLod != null)
			{
				camLod.fieldOfView = normalFOV;
			}
			cam.GetComponent<Camera>().fieldOfView = normalFOV;
		}
		if (OpticalAimHUD.instance != null)
		{
			OpticalAimHUD.instance.SetActiveModeAim(false);
			OpticalAimHUD.instance.SetActiveButAim(false);
			OpticalAimHUD.instance.stateOpticalActive = false;
		}
	}
}
